<%--                                                                       --%>
<%-- Copyright (c) 2001-2004 BroadVision, Inc.  All rights reserved.       --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>

<%@ page import =  "com.broadvision.web.util.ViewDynaBean,
		    org.apache.struts.validator.DynaValidatorForm,
                    org.apache.commons.beanutils.PropertyUtils,
                    java.util.List"
%>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<script language="JavaScript">

//
//
//
function closeWindow() {
  self.close();
}

//
// A user has been selected. Pass the userAlias value back to
// the parent window's callback function 'setUserAliasNew()'. And then
// close the popup window.
//
function setUserAliasNew(theForm, index) {
  var parentFormName = theForm.parentFormName.value;
  var parentFieldName = theForm.parentFieldName.value;
  var fN = "theForm.elements['oneResult[" + index + "].firstName'].value";
  var firstName = eval(fN);  
  var lN = "theForm.elements['oneResult[" + index + "].lastName'].value";
  var lastName = eval(lN);
  var fullN = "theForm.elements['oneResult[" + index + "].fullName'].value";
  var fullName = eval(fullN);
  var uA = "theForm.elements['oneResult[" + index + "].userAlias'].value";
  var userAlias = eval(uA);

  // we are merging the original userFind.jsp with this file
  // the userFind.jsp only returns the user alias
  // to differentiate the two, we use the following test:
  if ((null != parentFieldName) &&
      ('from' != parentFieldName) &&
      ('to' != parentFieldName)) {
    window.opener.setUserAlias(parentFormName, parentFieldName, userAlias);
  }
  else {
    window.opener.setUserAliasNew(parentFormName, parentFieldName, firstName, lastName, fullName, userAlias);
  }
  window.close();
}

//
//
//
function selectUserAlias(theForm, index) {
  var uA = "theForm.elements['oneResult[" + index + "].userAlias'].value";
  var userAlias = eval(uA);
  theForm.selectedAlias.value = userAlias;
  theForm.submit();
}

//
//
//
function removeUserAlias(theForm, index) {
  var uA = "theForm.elements['oneResultForRemove[" + index + "].userAlias'].value";
  var userAlias = eval(uA);
  theForm.removeAlias.value = userAlias;
  theForm.submit();
}

//
//
//
function setSelectedUsers(theForm) {
  var parentFormName = theForm.parentFormName.value;
  var parentFieldName = theForm.parentFieldName.value;
  var users = new Array();
  var selected = theForm.selected.value;

  if (selected.length > 0) {
    users = selected.split("$#@!");
  }

  window.opener.setSelectedUsers(parentFormName, parentFieldName, users);
  window.close();
}

  function doSearch(theForm) {
    if (theForm != null) {
      theForm.currentPageNumber.value = 0;
    }
    return true;
  }
  
  function gotoFirstPage(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      thisForm.currentPageNumber.value = 1;
      thisForm.executeSearch.value = "true";
      thisForm.submit();
      return false;
    }
  }
  
  function gotoPreviousPage(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      var currentPageNumber = thisForm.currentPageNumber.value;
      currentPageNumber--;
      if (thisForm.entriesPerPage.value != <bean:write name="cmUserFindNewForm" property="entriesPerPage"/>) {
        currentPageNumber = 1;
      }
      if (currentPageNumber > 0) {
        thisForm.currentPageNumber.value = currentPageNumber;
        thisForm.submit();
      }
    }
  }
  
  function gotoNextPage(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      var currentPageNumber = thisForm.currentPageNumber.value;
      currentPageNumber++;
      if (thisForm.entriesPerPage.value != <bean:write name="cmUserFindNewForm" property="entriesPerPage"/>) {
        currentPageNumber = 1;
      }
      if (currentPageNumber <= thisForm.totalPageNumber.value) {
        thisForm.currentPageNumber.value = currentPageNumber;
        thisForm.submit();
      }
    }
  }

  function gotoLastPage(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      if (thisForm.entriesPerPage.value != <bean:write name="cmUserFindNewForm" property="entriesPerPage"/>) {
        thisForm.currentPageNumber.value = 1;
      }
      else {
        thisForm.currentPageNumber.value = thisForm.totalPageNumber.value;
      }
      thisForm.submit();
    }
  }


  function doGo(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      if (thisForm.entriesPerPage.value != <bean:write name="cmUserFindNewForm" property="entriesPerPage"/>) {
        thisForm.currentPageNumber.value = 1;
        return true;
      }
    }
    return false;
  }
  
  function checkNumberOnly(e) {
    var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
      return false;
    }
    else {
      return true;
    }
  }

  function doValidateEntriesPerPage(formName) {
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      if (thisForm.entriesPerPage == null) {
        return true;
      }
      var entriesPerPageInt = parseInt(thisForm.entriesPerPage.value);
      if (isNaN(entriesPerPageInt)) {
        alert("<bean:message bundle='cm.messages' key='cm.common.pagination.validation.itemsPerPage'/>");
        thisForm.entriesPerPage.select();
        thisForm.entriesPerPage.focus();
        return false;
      }
      if (thisForm.entriesPerPage.value < 1 || thisForm.entriesPerPage.value > 2147483647) {
        alert("<bean:message bundle='cm.messages' key='cm.common.pagination.validation.itemsPerPage'/>");
        thisForm.entriesPerPage.select();
        thisForm.entriesPerPage.focus();
        return false;
      }
      else {
        return true;
      }
    }
    return false;
  }  

</script>

<bean:define id="multiselect" name="cmUserFindNewView" property="multiselect"/>

<%
  String actionPath = "/cm/cmUserSearchPopup.do";
  if (((Boolean) multiselect).booleanValue()) {
    actionPath = "/bp/userMultiSearchPopup.do";
  }
%>
<html:form action="<%= actionPath %>"  onsubmit="return doGo('cmUserFindNewForm');">

<html:hidden property="parentFormName"/>
<html:hidden property="parentFieldName"/>
<html:hidden property="selected"/>
<html:hidden property="suggested"/>
<html:hidden property="selectedAlias"/>
<html:hidden property="removeAlias"/>
<html:hidden property="currentPageNumber"/>
<html:hidden property="totalPageNumber"/>

      <!-- Zone 5 starts here.  -->
      <table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
        <tr>
          <td valign="top" width="100%">
            <div class="generalWrapRight">
              <html:button property="closeWinBtn" styleClass="inputButton" onclick="javascript:closeWindow()">
                <bean:message bundle='cm.messages' key='button.close' />
              </html:button>
            </div> 

            <table class="labelForm" width="100%" border="0"
cellspacing="0" cellpadding="2" summary="User search criteria table">
              <tr> 
                <td width="10%" nowrap><b><bean:message bundle='cm.messages' key='cm.user.userFind.label.searchUser'/>:</b><br>&nbsp;</td>
                <logic:equal name="cmUserFindNewView"
property="useFullName" value="false">
                  <td width="10%" nowrap>
                    <html:text name="cmUserFindNewForm" property="firstName" size="20" maxlength="80" />
                    <br><i>(<bean:message bundle='cm.messages' key='cm.user.label.firstName'/>)</i>
                  </td>
                  <td width="10%" nowrap>
                    <html:text name="cmUserFindNewForm" property="lastName" size="20" maxlength="80" />
                    <br><i>(<bean:message bundle='cm.messages' key='cm.user.label.lastName'/>)</i>
                  </td>
                </logic:equal>

                <logic:equal name="cmUserFindNewView" property="useFullName" value="true">
                  <td width="20%" nowrap>
                    <html:text  name="cmUserFindNewForm" property="fullName" size="20" maxlength="80" />
                    <br><i>(<bean:message bundle='cm.messages' key='cm.user.label.fullName'/>)</i>
                  </td>
                </logic:equal>
                <td width="10%" nowrap>
                  <html:text  name="cmUserFindNewForm" property="userAlias" size="20" maxlength="80"/>
		  <br><i>(<bean:message bundle='cm.messages' key='cm.user.label.loginId'/>)</i>
                </td>
                <td width="60%" nowrap>
                  <input hspace="40" type="submit" name="btn_action" class="inputButton" value="<bean:message bundle='cm.messages' key='button.goto'/>" onclick="javascript: return gotoFirstPage('cmUserFindNewForm');">
                  <br>&nbsp;
                </td>
              </tr>
            </table>
			
<logic:equal name="cmUserFindNewForm" property="executeSearch" value="true">
            <!-- Start Result List here -->
            <%
              ViewDynaBean cmUserFindNewView = (ViewDynaBean) request.getAttribute("cmUserFindNewView");
              Integer searchResultListLength = (Integer) PropertyUtils.getSimpleProperty(cmUserFindNewView, "numOfUsers");
            %>

            <div class="pageSubtitle">
              <span class="left"><bean:message bundle='cm.messages' key='cm.user.userFind.searchResult' arg0="<%= searchResultListLength.toString() %>" /></span>
              <span class="right">&nbsp;</span>
            </div> 

            <table class="dataTable" border="0" cellspacing="1" cellpadding="2" summary="Search result table" width="100%">
              <tr class="listHeader">
                <th id="Name" align="left"><bean:message bundle='cm.messages' key='cm.user.userFind.header.name'/></th>
                <th id="Id" align="left"><bean:message bundle='cm.messages' key='cm.user.userFind.header.id'/></th>
                <th id="Actions" align="left" nowrap><bean:message bundle='cm.messages' key='cm.user.userFind.header.actions'/></th>
              </tr>

              <logic:empty name="cmUserFindNewView" property="searchResultList">
                <tr class="listEven">
                  <td colspan=3 align="center"><bean:message bundle='cm.messages' key='cm.user.userFind.noMatch'/></td>
                </tr>
              </logic:empty>
              <logic:notEmpty name="cmUserFindNewView" property="searchResultList">
                <logic:iterate id="oneResult" name="cmUserFindNewView" property="searchResultList" indexId="index">
                  <tr class='<%= index.intValue() % 2 == 0 ? "listEven" : "listOdd"%>'>
                    <td headers="Name">
                      <bean:write name="oneResult" property="name" />
                    </td>
                    <td headers="Id"><bean:write name="oneResult" property="userAlias"/>&nbsp;</td>
                    <td nowrap align="left" headers="Actions">

<html:hidden name="oneResult" property="userAlias" indexed="true"/>
<logic:equal name="cmUserFindNewView" property="multiselect" value="true">
                      <a href="#" onclick="return selectUserAlias(document.forms[0], '<bean:write name="index"/>');">
                        <img src="<%=request.getContextPath()%>/bp/images/action/icon_add.gif" width="15" height="15" border="0" alt='<bean:message bundle='cm.messages' key='image.alt.add'/>' title='<bean:message bundle='cm.messages' key='image.alt.add'/>' >
                      </a>
</logic:equal>

<logic:equal name="cmUserFindNewView" property="multiselect" value="false">
<html:hidden name="oneResult" property="firstName" indexed="true"/>
<html:hidden name="oneResult" property="lastName" indexed="true"/>
<html:hidden name="oneResult" property="fullName" indexed="true"/>
                      <a href="#" onclick="return setUserAliasNew(document.forms[0], '<bean:write name="index"/>')">
                        <img src="<%=request.getContextPath()%>/bp/images/action/icon_add.gif" width="15" height="15" border="0" alt='<bean:message bundle='cm.messages' key='image.alt.selectitem'/>' title='<bean:message bundle='cm.messages' key='image.alt.selectitem'/>' >
                      </a>
</logic:equal>
                    </td>
                  </tr>
                </logic:iterate>
              </logic:notEmpty>
            </table>
            <!--end result list here-->


      <logic:greaterThan name="cmUserFindNewView" property="numOfUsers" value="0">
        <div class="pagination">
          <bean:message bundle='cm.messages' key='cm.common.pagination.show'/>
          <html:text name="cmUserFindNewForm" property="entriesPerPage" size="10" maxlength="10" onkeypress="return checkNumberOnly(event)"/>
          <bean:message bundle='cm.messages' key='cm.common.pagination.itemsPerPage'/>
          &nbsp;
          <html:submit property="go" styleClass="inputButton"
            onclick="return doGo('cmUserFindNewForm');">
            <bean:message bundle='cm.messages' key='button.goto'/>
          </html:submit>
          <logic:greaterThan name="cmUserFindNewForm" property="totalPageNumber" value="1">
            &nbsp;&nbsp;&nbsp;
            <logic:greaterThan name="cmUserFindNewForm" property="currentPageNumber" value="0">
              <logic:greaterThan name="cmUserFindNewForm" property="totalPageNumber" value="1">
                <logic:greaterThan name="cmUserFindNewForm" property="currentPageNumber" value="1">
                  <a href="#" class="paginationBtn" onclick="javascript:gotoFirstPage('cmUserFindNewForm')">
                    <bean:message bundle='cm.messages' key='cm.common.pagination.first'/></a>
                  <a href="#" class="paginationBtn" onclick="javascript:gotoPreviousPage('cmUserFindNewForm')">
                    <bean:message bundle='cm.messages' key='cm.common.pagination.previous'/></a>
                </logic:greaterThan>
              </logic:greaterThan>
              <bean:define id="currentPageNumberAttr" name="cmUserFindNewForm" property="currentPageNumber"/>
              <bean:define id="totalPageNumberAttr" name="cmUserFindNewForm" property="totalPageNumber"/>
              <% Integer currentPageNumberInt = (Integer)pageContext.getAttribute("currentPageNumberAttr"); %>
              <% Integer totalPageNumberInt = (Integer)pageContext.getAttribute("totalPageNumberAttr"); %>
              <bean:message bundle='cm.messages' key='cm.common.pagination.pageOf' 
                arg0="<%= currentPageNumberInt.toString() %>" 
                arg1="<%= totalPageNumberInt.toString() %>"/>
              <logic:greaterThan name="cmUserFindNewForm" property="totalPageNumber" value="1">
                <% if (currentPageNumberInt.intValue() < totalPageNumberInt.intValue()) { %>
                  <a href="#" class="paginationBtn" onclick="javascript:gotoNextPage('cmUserFindNewForm')">
                    <bean:message bundle='cm.messages' key='cm.common.pagination.next'/></a>
                  <a href="#" class="paginationBtn" onclick="javascript:gotoLastPage('cmUserFindNewForm')">
                    <bean:message bundle='cm.messages' key='cm.common.pagination.last'/></a>
                <% } %>
              </logic:greaterThan>
            </logic:greaterThan>
          </logic:greaterThan>
        </div>
      </logic:greaterThan>


</logic:equal>
<!-- for executeSearch -->
          </td>
        </tr>

<logic:equal name="cmUserFindNewView" property="multiselect" value="true">

        <tr>
          <td>

            <div class="pageSubtitle">
              <span class="left"><bean:message bundle='cm.messages' key='cm.user.searchBody.selectedUsersSubtitle'/></span>
              <span class="right">&nbsp;</span>
            </div>


            <table class="dataTable" border="0" cellspacing="1" cellpadding="2" summary="Search result table" width="100%">
              <tr class="listHeader">
                <th id="Name" align="left"><bean:message bundle='cm.messages' key='cm.user.userFind.header.name'/></th>
                <th id="Id" align="left"><bean:message bundle='cm.messages' key='cm.user.userFind.header.id'/></th>
                <th id="Actions" align="left" nowrap><bean:message bundle='cm.messages' key='cm.user.userFind.header.actions'/></th>
              </tr>
              <logic:empty name="selectedUserView" property="selectedList">
                <tr class="listEven">
                  <td colspan=3 align="center"><bean:message bundle='cm.messages' key='cm.user.userFind.noSelectedUsers'/></td>
                </tr>
              </logic:empty>

              <logic:notEmpty name="selectedUserView" property="selectedList">
                <logic:iterate id="oneResultForRemove" name="selectedUserView" property="selectedList" indexId="index">
                  <tr class='<%= index.intValue() % 2 == 0 ? "listEven" : "listOdd"%>'>
                    <td headers="Name">
                      <bean:write name="oneResultForRemove" property="name" />
                    </td>
                    <td headers="Id"><bean:write name="oneResultForRemove" property="userAlias"/>&nbsp;</td>
                    <td nowrap align="left" headers="Actions">
                      <html:hidden name="oneResultForRemove" property="userAlias" indexed="true"/>
                      <a href="JavaScript:removeUserAlias(document.forms[0], '<bean:write name="index"/>')">
                      <img src="<%=request.getContextPath()%>/bp/images/action/icon_remove.gif" width="15" height="15" border="0" alt='<bean:message bundle='cm.messages' key='image.alt.remove'/>' title='<bean:message bundle='cm.messages' key='image.alt.remove'/>' >
                      </a>
                    </td>
                  </tr>
                </logic:iterate>
              </logic:notEmpty>
            </table>

            <div class="pageButtonWrap" align="right">
              <input hspace="40" type="button" name="btn_action" class="pageButton" value="<bean:message bundle='cm.messages' key='button.done'/>" onClick="JavaScript:setSelectedUsers(document.forms[0])">
            </div>

          </td>
        </tr>
</logic:equal>
      </table>

      <!--end zone 5 content here -->
<logic:empty name="cmUserFindNewForm" property="suggested">
      <input type="hidden" name="executeSearch" value="true"/>
</logic:empty>
<logic:notEmpty name="cmUserFindNewForm" property="suggested">
      <input type="hidden" name="executeSearch" value="false"/>
</logic:notEmpty>

</html:form>

